import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

/* 重写 push 方法，解决多次跳转当前路由地址或当前路由携带参数并且参数不变多次跳转当前路由警告报错 */
const originPush = VueRouter.prototype.push

VueRouter.prototype.push = function(location, onResolve, onReject) {
  if (onResolve || onReject) {
    return originPush.call(this, location, onResolve, onReject)
  }
  return originPush.call(this, location).catch((err) => err)
}

/* 重写 replace 方法，解决多次跳转当前路由地址或当前路由携带参数并且参数不变多次跳转当前路由警告报错 */
const originReplace = VueRouter.prototype.replace

VueRouter.prototype.replace = function(location, onResolve, onReject) {
  if (onResolve || onReject) {
    return originReplace.call(this, location, onResolve, onReject)
  }
  return originReplace.call(this, location).catch((err) => err)
}

const routes = [
  {
    path: '/',
    redirect: '/home',
    meta: { isShow: true }
  },
  // 首页
  {
    path: '/home',
    component: () =>
      import(/* webpackChunkName: "HomeGroup" */ '@/views/Home.vue'),
    meta: { isShow: true }
  },
  // 登录页面
  {
    path: '/login',
    component: () =>
      import(/* webpackChunkName: "LoginGroup" */ '@/views/Login.vue'),
    meta: { isShow: false }
  },
  // 注册页面
  {
    path: '/register',
    component: () =>
      import(/* webpackChunkName: "RegisterGroup" */ '@/views/Register.vue'),
    meta: { isShow: false }
  },
  // 搜索页面
  {
    path: '/search/:searchValue?',
    component: () =>
      import(/* webpackChunkName: "SearchGroup" */ '@/views/Search.vue'),
    meta: { isShow: true },
    name: 'Search'
  }
]

const router = new VueRouter({
  routes
})

export default router
